﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.WebComponent
Imports Lsk.Business
Imports Lsk.Data
Imports FlickrNet


Partial Class addPhoto
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

            If Not Session("PropertyID") Is Nothing Then
                lblPropertyID.Text = Session.Item("PropertyID").ToString
            Else
                Response.Redirect("propertylist.aspx", True)
            End If

            If Not Session("Address") Is Nothing Then
                lblAddress.Text = Session.Item("Address").ToString
            End If

            If Not Session("tp") Is Nothing Then
                lblhidListType.Text = Session.Item("TP").ToString
            End If

            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Dim PropertyDT As New Data.DataTable


            'Getting Property Information
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "SELECT AW_Project.Project, AW_PropertyType.Type, " & _
                                             "CONVERT(nvarchar(21), AW_Property.LAWidth) + ' X ' + CONVERT(nvarchar(21), AW_Property.LALength) + ' = ' + CONVERT(nvarchar(21), AW_Property.LASF) AS 'LASQ', " & _
                                             "CONVERT(nvarchar(21), AW_Property.BUWidth) + ' X ' + CONVERT(nvarchar(21), AW_Property.BULength) + ' = ' + CONVERT(nvarchar(21), AW_Property.BUSF) AS 'BUSQ', " & _
                                             "AW_Property.SRAmt,AW_Property.SRPSF,AW_Property.SRAskAmt,AW_Property.SRAskPSF,AW_Property.SRCreateDate,AW_Property.SRYes,AW_Property.SCAmt,AW_Property.SCPSF,AW_Property.SCAskAmt,AW_Property.SCAskPSF,AW_Property.SCCreateDate,AW_Property.SCYes, " & _
                                             "AW_Property.SLAmt,AW_Property.SLPSF,AW_Property.SLAskAmt,AW_Property.SLAskPSF,AW_Property.SLCreateDate,AW_Property.SLYes,AW_Property.SIAmt,AW_Property.SIPSF,AW_Property.SIAskAmt,AW_Property.SIAskPSF,AW_Property.SICreateDate,AW_Property.SIYes, " & _
                                             "AW_Property.RRAmt,AW_Property.RRPSF,AW_Property.RRAskAmt,AW_Property.RRAskPSF,AW_Property.RRCreateDate,AW_Property.RRYes,AW_Property.RCAmt,AW_Property.RCPSF,AW_Property.RCAskAmt,AW_Property.RCAskPSF,AW_Property.RCCreateDate,AW_Property.RCYes, " & _
                                             "AW_Property.RLAmt,AW_Property.RLPSF,AW_Property.RLAskAmt,AW_Property.RLAskPSF,AW_Property.RLCreateDate,AW_Property.RLYes,AW_Property.RIAmt,AW_Property.RIPSF,AW_Property.RIAskAmt,AW_Property.RIAskPSF,AW_Property.RICreateDate,AW_Property.RIYes " & _
                                             "FROM AW_Property LEFT OUTER JOIN AW_Project ON AW_Property.ProjectId=AW_Project.ProjectId LEFT OUTER JOIN AW_PropertyType ON AW_Property.PropertyTypeId=TypeId " & _
                                             "WHERE AW_Property.PropertyId=@PropertyId"
                objDB.AddParameter("PropertyId", Data.SqlDbType.UniqueIdentifier, New Guid(lblPropertyID.Text))
                objDB.FillData(PropertyDT, "Property", Database.SQLCommandType.NormalString)


                'Begin Populating Porperty Information
                For Each ReadRow As Data.DataRow In PropertyDT.Rows
                    Select Case lblhidListType.Text
                        Case "1" '--Sale
                            If ReadRow.Item("SRYes").ToString = "1" Then
                                trSR.Visible = True
                            Else
                                trSR.Visible = False
                            End If
                            If ReadRow.Item("SCYes").ToString = "1" Then
                                trSC.Visible = True
                            Else
                                trSC.Visible = False
                            End If
                            If ReadRow.Item("SLYes").ToString = "1" Then
                                trSL.Visible = True
                            Else
                                trSL.Visible = False
                            End If
                            If ReadRow.Item("SIYes").ToString = "1" Then
                                trSI.Visible = True
                            Else
                                trSI.Visible = False
                            End If
                            trRR.Visible = False
                            trRC.Visible = False
                            trRL.Visible = False
                            trRI.Visible = False
                        Case "2" '--Rent
                            If ReadRow.Item("RRYes").ToString = "1" Then
                                trRR.Visible = True
                            Else
                                trRR.Visible = False
                            End If
                            If ReadRow.Item("RCYes").ToString = "1" Then
                                trRC.Visible = True
                            Else
                                trRC.Visible = False
                            End If
                            If ReadRow.Item("RLYes").ToString = "1" Then
                                trRL.Visible = True
                            Else
                                trRL.Visible = False
                            End If
                            If ReadRow.Item("RIYes").ToString = "1" Then
                                trRI.Visible = True
                            Else
                                trRI.Visible = False
                            End If
                            trSR.Visible = False
                            trSC.Visible = False
                            trSL.Visible = False
                            trSI.Visible = False
                        Case "3"
                            If ReadRow.Item("SRYes").ToString = "1" Then
                                trSR.Visible = True
                            Else
                                trSR.Visible = False
                            End If
                            If ReadRow.Item("SCYes").ToString = "1" Then
                                trSC.Visible = True
                            Else
                                trSC.Visible = False
                            End If
                            If ReadRow.Item("SLYes").ToString = "1" Then
                                trSL.Visible = True
                            Else
                                trSL.Visible = False
                            End If
                            If ReadRow.Item("SIYes").ToString = "1" Then
                                trSI.Visible = True
                            Else
                                trSI.Visible = False
                            End If
                            If ReadRow.Item("RRYes").ToString = "1" Then
                                trRR.Visible = True
                            Else
                                trRR.Visible = False
                            End If
                            If ReadRow.Item("RCYes").ToString = "1" Then
                                trRC.Visible = True
                            Else
                                trRC.Visible = False
                            End If
                            If ReadRow.Item("RLYes").ToString = "1" Then
                                trRL.Visible = True
                            Else
                                trRL.Visible = False
                            End If
                            If ReadRow.Item("RIYes").ToString = "1" Then
                                trRI.Visible = True
                            Else
                                trRI.Visible = False
                            End If
                    End Select
                    lblProject.Text = ReadRow.Item("Project").ToString
                    lblPropertyType.Text = ReadRow.Item("Type").ToString
                    lblLASQ.Text = String.Format("{0:#,##0.00}", ReadRow.Item("LASQ"))
                    lblBUSQ.Text = String.Format("{0:#,##0.00}", ReadRow.Item("BUSQ"))
                    lblSRSales.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SRAmt"))
                    lblSRPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SRPSF"))
                    lblSRAsk.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SRAskAmt"))
                    lblSRAskPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SRAskPSF"))
                    If Not ReadRow.Item("SRCreateDate").ToString = "" Then
                        lblSRDateCreate.Text = Format(ReadRow.Item("SRCreateDate"), "dd-MMM-yyyy")
                    End If
                    'lblSCSales.Text = Format(ReadRow.Item("SCAmt"), "{0:#,##0.00}")
                    lblSCSales.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SCAmt"))
                    lblSCPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SCPSF"))
                    lblSCAsk.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SCAskAmt"))
                    lblSCAskPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SCAskPSF"))
                    If Not ReadRow.Item("SCCreateDate").ToString = "" Then
                        lblSCDateCreate.Text = String.Format("{0:dd-MMM-yyyy}", ReadRow.Item("SCCreateDate"))
                    End If
                    lblSLSales.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SLAmt"))
                    lblSLPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SLPSF"))
                    lblSLAsk.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SLAskAmt"))
                    lblSLAskPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SLAskPSF"))
                    If Not ReadRow.Item("SLCreateDate").ToString = "" Then
                        lblSLDateCreate.Text = String.Format("{0:dd-MMM-yyyy}", ReadRow.Item("SLCreateDate"))
                    End If
                    lblSISales.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SIAmt"))
                    lblSIPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SIPSF"))
                    lblSIAsk.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SIAskAmt"))
                    lblSIAskPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("SIAskPSF"))
                    If Not ReadRow.Item("SICreateDate").ToString = "" Then
                        lblSIDateCreate.Text = Format(ReadRow.Item("SICreateDate"), "dd-MMM-yyyy")
                    End If
                    '--
                    lblRRSales.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RRAmt"))
                    lblRRPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RRPSF"))
                    lblRRAsk.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RRAskAmt"))
                    lblRRAskPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RRAskPSF"))
                    If Not ReadRow.Item("RRCreateDate").ToString = "" Then
                        lblRRDateCreate.Text = Format(ReadRow.Item("RRCreateDate"), "dd-MMM-yyyy")
                    End If
                    lblRCSales.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RCAmt"))
                    lblRCPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RCPSF"))
                    lblRCAsk.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RCAskAmt"))
                    lblRCAskPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RCAskPSF"))
                    If Not ReadRow.Item("RCCreateDate").ToString = "" Then
                        lblRCDateCreate.Text = Format(ReadRow.Item("RCCreateDate"), "dd-MMM-yyyy")
                    End If
                    lblRLSales.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RLAmt"))
                    lblRLPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RLPSF"))
                    lblRLAsk.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RLAskAmt"))
                    lblRLAskPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RLAskPSF"))
                    If Not ReadRow.Item("RLCreateDate").ToString = "" Then
                        lblRLDateCreate.Text = Format(ReadRow.Item("RLCreateDate"), "dd-MMM-yyyy")
                    End If
                    lblRISales.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RIAmt"))
                    lblRIPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RIPSF"))
                    lblRIAsk.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RIAskAmt"))
                    lblRIAskPSF.Text = String.Format("{0:#,##0.00}", ReadRow.Item("RIAskPSF"))
                    If Not ReadRow.Item("RICreateDate").ToString = "" Then
                        lblRIDateCreate.Text = Format(ReadRow.Item("RICreateDate"), "dd-MMM-yyyy")
                    End If
                Next

                LoadPhoto(objDB)
                LoadUploadPhoto()
            Catch
                divError.Visible = True
                lblMsgError.Text = "Error getting data from Database."
                Return
            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try




        End If


    End Sub

    Private Sub LoadPhoto(ByVal objDB As Database)
        Dim PictureDT As New Data.DataTable
        'Getting Current Picture Information
        objDB.dbVariable.SQLString = "SELECT PictureId,Picture,pictureURL,PictureDesc, isThumbnail FROM AW_Picture WHERE PropertyId=@PropertyId"
        objDB.AddParameter("PropertyId", Data.SqlDbType.UniqueIdentifier, New Guid(lblPropertyID.Text))
        objDB.FillData(PictureDT, "Picture", Database.SQLCommandType.NormalString)
        objDB.ClearParameter()

        'Populating Images
        dlUploadedPhoto.DataSource = PictureDT
        dlUploadedPhoto.DataBind()

    End Sub

    Private Sub LoadUploadPhoto()
        'Setting Up Datatable for Picture Upload(5 row for initial Upload) 
        'Note this datatable is merely for multiple upload data in this datatable has will be saved into the database
        Dim PictureUploadDt As New Data.DataTable
        PictureUploadDt.Columns.Add(New Data.DataColumn("Picture"))
        PictureUploadDt.Columns.Add(New Data.DataColumn("IsThumbnail"))

        Dim NewRow As Data.DataRow

        For nLoop As Integer = 1 To 5
            NewRow = PictureUploadDt.NewRow
            NewRow.Item("Picture") = Guid.NewGuid.ToString
            NewRow.Item("IsThumbnail") = 0
            PictureUploadDt.Rows.Add(NewRow)
        Next

        lblPhotoCount.Text = 5

        repPhoto.DataSource = PictureUploadDt
        repPhoto.DataBind()
    End Sub

    Protected Sub btnAddMore_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddMore.Click

        Dim PictureUploadDt As New Data.DataTable
        PictureUploadDt.Columns.Add(New Data.DataColumn("Picture"))
        PictureUploadDt.Columns.Add(New Data.DataColumn("IsThumbnail"))

        Dim NewRow As Data.DataRow

        For nLoop As Integer = 1 To CInt(lblPhotoCount.Text + 5)
            NewRow = PictureUploadDt.NewRow
            NewRow.Item("Picture") = Guid.NewGuid.ToString
            NewRow.Item("IsThumbnail") = 0
            PictureUploadDt.Rows.Add(NewRow)
        Next

        lblPhotoCount.Text = CInt(lblPhotoCount.Text + 5)
        repPhoto.DataSource = PictureUploadDt
        repPhoto.DataBind()
    End Sub

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click

        'Begin Uploading Process
        Dim UploadedPhotoImg As String = ""
        Dim UploadPhotoError As String = ""
        Dim PhotoImg As HttpPostedFile
        Dim PhotoDT As New Data.DataTable
        Dim NewPhotoRow As Data.DataRow
        Dim ErrorMEssage As String = ""

        PhotoDT.Columns.Add(New Data.DataColumn("PictureId"))
        PhotoDT.Columns.Add(New Data.DataColumn("Picture"))
        PhotoDT.Columns.Add(New Data.DataColumn("PropertyId"))
        PhotoDT.Columns.Add(New Data.DataColumn("isThumbnail"))
        PhotoDT.Columns.Add(New Data.DataColumn("PhotoId"))
        PhotoDT.Columns.Add(New Data.DataColumn("photoURL"))
        PhotoDT.Columns.Add(New Data.DataColumn("LargeUrl"))
        PhotoDT.Columns.Add(New Data.DataColumn("MediumUrl"))
        PhotoDT.Columns.Add(New Data.DataColumn("SmallUrl"))
        'TODO : Upload to Flickr
        For nLoop As Integer = 0 To repPhoto.Items.Count - 1
            Try
                If CType(repPhoto.Items(nLoop).FindControl("filRepPhoto"), FileUpload).HasFile Then
                    PhotoImg = CType(repPhoto.Items(nLoop).FindControl("filRepPhoto"), FileUpload).PostedFile
                    If System.IO.Path.GetExtension(PhotoImg.FileName).ToLower() <> ".jpg" And System.IO.Path.GetExtension(PhotoImg.FileName).ToLower() <> ".png" And System.IO.Path.GetExtension(PhotoImg.FileName).ToLower() <> ".png" Then
                        UploadPhotoError = UploadPhotoError & PhotoImg.FileName & " can only be JPG,GIF and PNG format<br />"
                    Else
                       
                        Dim objImgComp As New ImageComp
                        UploadedPhotoImg = objImgComp.UploadSingleImage(PhotoImg, "property", False, 150)
                        NewPhotoRow = PhotoDT.NewRow
                        NewPhotoRow.Item("PictureId") = Guid.NewGuid.ToString
                        NewPhotoRow.Item("Picture") = UploadedPhotoImg
                        NewPhotoRow.Item("PropertyId") = lblPropertyID.Text
                        NewPhotoRow.Item("isThumbnail") = 0


                        'Upload to Flickr

                        Dim pMgr As New PhotoManager
                        Dim picture As New AW_Picture
                        Dim flickInfo As New PhotoInfo
                        picture.Picture = UploadedPhotoImg
                        picture.PropertyId = New Guid(lblPropertyID.Text)
                        picture.PhotoId = NewPhotoRow.Item("PropertyId")
                        picture.PictureDesc = lblPropertyID.Text
                        flickInfo = pMgr.Upload(PhotoImg.InputStream, picture)
                        NewPhotoRow.Item("PhotoId") = flickInfo.PhotoId
                        NewPhotoRow.Item("photoURL") = flickInfo.OriginalUrl
                        NewPhotoRow.Item("LargeUrl") = flickInfo.LargeUrl
                        NewPhotoRow.Item("MediumUrl") = flickInfo.MediumUrl
                        NewPhotoRow.Item("SmallUrl") = flickInfo.SmallUrl


                        PhotoDT.Rows.Add(NewPhotoRow)

                    End If
                End If
            Catch ex As Exception
                UploadPhotoError = UploadPhotoError & CType(repPhoto.Items(nLoop).FindControl("filRepPhoto"), FileUpload).PostedFile.FileName & " has error with error message : " & ex.Message & "<br />"
            End Try
        Next

        'Begin Saving Photo Information into Database
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))

        Try
            objDB.OpenConnection()
            objDB.InitDbTrans()

            'Clearing Thumbnail of Previous
            'objDB.dbVariable.SQLString = "UPDATE AW_Picture SET isThumbnail=0 WHERE PropertyId=@PropertyId"
            'objDB.AddParameter("PropertyId", Data.SqlDbType.UniqueIdentifier, New Guid(lblPropertyID.Text))
            'objDB.ExecuteQuery()

            objDB.dbVariable.SQLString = "INSERT INTO AW_Picture (PictureId,Picture,PropertyId,isThumbnail,CreateDate,PhotoId,pictureURL,LargeUrl,MediumUrl,SmallUrl) VALUES (@PictureId,@Picture,@PropertyId,@isThumbnail,@CreateDate,@PhotoId,@photoURL,@LargeUrl,@MediumUrl,@SmallUrl)"
            For Each ReadRow As Data.DataRow In PhotoDT.Rows
                objDB.ClearParameter()
                objDB.AddParameter("PictureId", Data.SqlDbType.UniqueIdentifier, New Guid(ReadRow.Item("PictureId").ToString))
                objDB.AddParameter("Picture", Data.SqlDbType.NVarChar, ReadRow.Item("Picture").ToString)
                objDB.AddParameter("PropertyId", Data.SqlDbType.NVarChar, ReadRow.Item("PropertyId").ToString)
                objDB.AddParameter("isThumbnail", Data.SqlDbType.TinyInt, CInt(ReadRow.Item("isThumbnail").ToString))
                objDB.AddParameter("CreateDate", Data.SqlDbType.DateTime, Now)
                objDB.AddParameter("PhotoId", Data.SqlDbType.NVarChar, ReadRow.Item("PhotoId").ToString)
                objDB.AddParameter("photoURL", Data.SqlDbType.NVarChar, ReadRow.Item("photoURL").ToString)
                objDB.AddParameter("LargeUrl", Data.SqlDbType.NVarChar, ReadRow.Item("LargeUrl").ToString)
                objDB.AddParameter("MediumUrl", Data.SqlDbType.NVarChar, ReadRow.Item("MediumUrl").ToString)
                objDB.AddParameter("SmallUrl", Data.SqlDbType.NVarChar, ReadRow.Item("SmallUrl").ToString)

                objDB.ExecuteQuery()
            Next
            objDB.DbTransCommit()
            LoadPhoto(objDB)
            objDB.CloseConnection()

        Catch ex As Exception
            If objDB.dbVariable.objConn.State = Data.ConnectionState.Open Then
                objDB.DbTransRollback()
                objDB.CloseConnection()
            End If

            'Begin Removing Picture if Saving to database failed
            For Each Readrow As Data.DataRow In PhotoDT.Rows
                Dim objImgComp As New ImageComp
                objImgComp.DeleteSingleFile("property", Readrow.Item("Picture"))
            Next

            lblMsgError.Text = "There is an error with the following message : " & ex.Message & "<br />"
            ErrorMEssage = lblMsgError.Text
        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try

        ErrorMEssage = ErrorMEssage & UploadPhotoError
        If ErrorMEssage <> "" Then
            divError.Visible = True
            lblMsgError.Text = ErrorMEssage
        Else
            divSuccessful.Visible = True
            lblMsgSuccessful.Text = "Photo Uploaded"
        End If

    End Sub

    Protected Sub btnSaveDescription_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveDescription.Click

        'Begin Updating Description
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))

        Try
            objDB.OpenConnection()
            objDB.InitDbTrans()
            objDB.dbVariable.SQLString = "UPDATE AW_Picture SET PictureDesc=@PictureDesc,isThumbnail=@isThumbnail WHERE PictureId=@PictureId"
            For nLoop As Integer = 0 To dlUploadedPhoto.Items.Count - 1
                If CType(dlUploadedPhoto.Items(nLoop).FindControl("txtDLPictureDesc"), TextBox).Text.Trim <> "" Then
                    objDB.ClearParameter()
                    Dim rbListControls As RadioButtonList = DirectCast((dlUploadedPhoto.Items(nLoop).FindControl("rblSelectedPicture")), RadioButtonList)
                    objDB.AddParameter("isThumbnail", Data.SqlDbType.TinyInt, rbListControls.SelectedValue)

                    'If CType(dlUploadedPhoto.Items(nLoop).FindControl("rbtRepPhoto"), CheckBox).Checked = True Then
                    '    objDB.AddParameter("isThumbnail", Data.SqlDbType.TinyInt, 1)
                    'Else
                    '    objDB.AddParameter("isThumbnail", Data.SqlDbType.TinyInt, 0)
                    'End If
                    objDB.AddParameter("PictureId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(dlUploadedPhoto.Items(nLoop).FindControl("imgRepDel"), ImageButton).CommandArgument))
                    objDB.AddParameter("PictureDesc", Data.SqlDbType.NVarChar, CType(dlUploadedPhoto.Items(nLoop).FindControl("txtDLPictureDesc"), TextBox).Text.Trim)
                    objDB.ExecuteQuery()
                Else
                    objDB.ClearParameter()
                    Dim rbListControls As RadioButtonList = DirectCast((dlUploadedPhoto.Items(nLoop).FindControl("rblSelectedPicture")), RadioButtonList)
                    objDB.AddParameter("isThumbnail", Data.SqlDbType.TinyInt, rbListControls.SelectedValue)
                    'If CType(dlUploadedPhoto.Items(nLoop).FindControl("rbtRepPhoto"), CheckBox).Checked = True Then
                    '    objDB.AddParameter("isThumbnail", Data.SqlDbType.TinyInt, 1)
                    'Else
                    '    objDB.AddParameter("isThumbnail", Data.SqlDbType.TinyInt, 0)
                    'End If
                    objDB.AddParameter("PictureId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(dlUploadedPhoto.Items(nLoop).FindControl("imgRepDel"), ImageButton).CommandArgument))
                    objDB.AddParameter("PictureDesc", Data.SqlDbType.NVarChar, DBNull.Value)
                    objDB.ExecuteQuery()
                End If
            Next
            objDB.DbTransCommit()
            objDB.CloseConnection()
            lblMsgSuccessful.Text = "Photo Description Update"
            divSuccessful.Visible = True
        Catch ex As Exception
            If objDB.dbVariable.objConn.State = Data.ConnectionState.Open Then
                objDB.DbTransRollback()
                objDB.CloseConnection()
            End If
            lblMsgError.Text = "There is an error updating Photo Description with the following error message : " & ex.Message
            divError.Visible = True
        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
    End Sub

    Protected Sub dlUploadedPhoto_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlUploadedPhoto.ItemCommand

        If e.CommandName = "DeletePicture" Then
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "DELETE FROM AW_Picture WHERE PictureId=@PictureId"
                objDB.AddParameter("PictureId", Data.SqlDbType.UniqueIdentifier, New Guid(e.CommandArgument.ToString))
                Dim objPicture As New ImageComp
                objPicture.DeleteSingleFile("property", CType(e.Item.FindControl("lblPicture"), Label).Text)
                objDB.ExecuteQuery()
                LoadPhoto(objDB)
                objDB.CloseConnection()
                divSuccessful.Visible = True
                lblMsgSuccessful.Text = "Photo Deleted"
            Catch ex As Exception
                If objDB.dbVariable.objConn.State = Data.ConnectionState.Open Then
                    objDB.CloseConnection()
                End If
                lblMsgError.Text = "There is an error updating Photo Description with the following error message : " & ex.Message
                divError.Visible = True
            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
        End If
    End Sub

    Protected Sub btnDone_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDone.Click
        Response.Redirect("propertylist.aspx")
    End Sub

    Protected Sub rblSelectedPicture_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim rbList As RadioButtonList = DirectCast(sender, RadioButtonList)
        If (rbList.SelectedIndex = 0) Then
            For nLoop As Integer = 0 To dlUploadedPhoto.Items.Count - 1
                Dim rbListControls As RadioButtonList = DirectCast((dlUploadedPhoto.Items(nLoop).FindControl("rblSelectedPicture")), RadioButtonList)
                If (rbList.ClientID <> rbListControls.ClientID) Then
                    rbListControls.SelectedIndex = 1
                End If
            Next
        End If


    End Sub
End Class
